const fs = require("fs");
const path = require("path");

const log   = console.log,
      stdin = process.stdin,
      stdout= process.stdout,
      msg   = ['请输入要创建的文件夹：', '请输入要创建的文件：', '请输入要删除的文件'];

let tip="", i = 1;

stdout.write(msg[0] + ': ');

stdin.on('data', (data) => {
    tip = data.slice(0, data.length - 1).toString('utf8');
    switch(tip.slice(0,5)){
        case "mkdir":
            var filePath = path.join(__dirname,"/"+tip.slice(6,tip.length-1));
            if(!fs.existsSync(filePath)){
                fs.mkdirSync(filePath);
                log("文件目录创建成功！！");
            }else{
                log("该文件夹名已被占用")
            }
            break;
        case "touch":
            filePath = path.join(__dirname,"/filedir/"+tip.slice(6,tip.length-1));
            if(!fs.existsSync(filePath)){
                fs.writeFile(filePath,"hello node",function(err){
                    if(err){
                        console.log(err);
                    }
                })
                log("文件创建成功");
            }else{
                log("该文件名已被占用")
            }
            break;
        case "delet":
            filePath = path.join(__dirname,"/filedir/"+tip.slice(7,tip.length-1));
            if(fs.existsSync(filePath)){
                fs.unlinkSync(filePath);
                log("删除成功");
            }else{
                log("没有这个文件")
            }
            process.exit();
            break;
    }
    stdout.write(msg[i++] + ': ');
});

stdin.on('end', () => {
    log("程序结束");
});